home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / bd3.zip / BD3.SPR < prev    next >
Text File  |  1989-04-30  |  54KB  |  914 lines

  1. STYLE LeftMargin 12,RightMargin 90,offset 0,Paper 11 in,PaperWidth 8.5 in,BottomMargin 6,TopMargin 6
  2. R 80,J,T 5
  3. CENTERPAGE .5 page
  4. BANK DEMO 3
  5.  
  6. A Demonstration of Tasking in Ada
  7.  
  8.  
  9.  
  10.  
  11. Version 1.0, 11 March 1989
  12.  
  13. by
  14. Richard Conn
  15.  
  16.  
  17.  
  18.  
  19.  
  20.     Bank Demo 3, or BD3 as the files and mainline procedure are named, is ademonstration program which illustrates how Ada and its tasking mechanism can beused to create a model of an activity in the real world.  BD3 is a model of abank, where the bank contains several tellers, implemented as Ada tasks, and isused by customers, also implemented as Ada tasks.  The user at the console canmonitor the status of the bank and the accounts of its customers and cause morecustomer tasks to execute, thereby increasing the level of activity at the bank.The tasks all operate in parallel, so a variety of events can be thought of ashappening at the same time, controlled by the Ada runtime system which is builtinto the Ada program by the Ada compilation system.
  21.  
  22.     This document and the associated software are presented as an introductionto the Ada language and its tasking capabilities.  While the author attempted tomake this document as easy to follow as possible, a limited knowledge of Ada isassumed.  Variable assignments, type definitions, and procedure and packagespecifications are presented in correct Ada syntax without much explanation.  Ifthis proves to be a problem, the ADA-TUTR shareware program will help a lot inbringing the user up to date with an understanding of Ada syntax.  Regardless,the program in the file BD3.EXE can be run without any knowledge of Ada at all,and the tasking demonstration can be observed.
  23.  
  24.  
  25. SET page=1
  26. BEGIN HEADER
  27. BD3 - A Demonstration of Tasking in Ada
  28. END HEADER
  29.  
  30. BANK DEMO 3
  31. A Demonstration of Tasking in Ada
  32.  
  33. by Richard Conn
  34.  
  35.  
  36. RESERVE 4
  37. SECTION Introduction
  38.  
  39.     BD3 is a model of a bank.  The Ada package called BANK represents the bankitself, and the bank contains four tellers, represented by an array, namedTELLER, of four tasks.  BD3 is written in an object-oriented fashion in the Adaprogramming language.
  40.  
  41. BEGIN FIGURE
  42. ======================================================================
  43.  
  44.   ADD_NEW_CUSTOMER
  45.     -- to create a new account for a customer and return
  46.     -- an ID for that customer
  47.   GET_BALANCE
  48.     -- to return the balance of a customer's account
  49.   MAKE_DEPOSIT
  50.     -- to deposit money into a customer's account
  51.   MAKE_WITHDRAWAL
  52.     -- to withdraw money from a customer's account
  53.  
  54. CAPTION The bank tellers can process four kinds of transactions.
  55. TAG TRANSACTION=figure
  56.  
  57. ======================================================================
  58. END FIGURE
  59.  
  60.     Each task, TELLER(1) to TELLER(4), supports one entry point, calledREQUEST, which is used by tasks outside the BANK package to communicate with aTELLER(n) task and request a transaction.  Four transaction requests arerecognized by a TELLER(n) task, as shown in Figure TRANSACTION.
  61.  
  62. BEGIN FIGURE
  63. ======================================================================
  64.  
  65.   MY_ID : BANK.CUSTOMER_ID;
  66.     -- MY_ID is a variable which holds the customer ID of
  67.     -- this task (type CUSTOMER_ID is defined in the package BANK)
  68.   AMOUNT : BANK.DOLLAR;
  69.     -- type DOLLAR represents the unit of currency, as defined
  70.     -- by the package BANK, and the variable AMOUNT is used to
  71.     -- hold the money passed in to or out of the TELLER(n) task
  72.      ...
  73.   BANK.TELLER(1).REQUEST (MY_ID, BANK.ADD_NEW_CUSTOMER, AMOUNT);
  74.     -- this calls the REQUEST entry point for the task TELLER(1)
  75.     -- in the package BANK; the ID of the customer is passed
  76.     -- out of the TELLER(1) task in the variable MY_ID and the
  77.     -- requested transaction is ADD_NEW_CUSTOMER, which is
  78.     -- defined in the package BANK; the variable AMOUNT is
  79.     -- passed as a parameter but not used
  80.   AMOUNT := 100.00;
  81.   BANK.TELLER(2).REQUEST (MY_ID, BANK.MAKE_DEPOSIT, AMOUNT);
  82.     -- the variable AMOUNT is set to $100, and the REQUEST
  83.     -- entry point of TELLER(2) in the package BANK is
  84.     -- called; the variable MY_ID, which was set by the call
  85.     -- to TELLER(1) above, is used to identify the customer,
  86.     -- and the requested action is MAKE_DEPOSIT, as defined by
  87.     -- the package BANK; AMOUNT is passed to the TELLER(2) task
  88.     -- as the amount to deposit
  89.  
  90. CAPTION This code segment shows that the TELLER(n) tasks are accessed likeprocedures.
  91. TAG CALLTELLER=figure
  92.  
  93. ======================================================================
  94. END FIGURE
  95.  
  96.     Each TELLER(n) task performs one of these four transactions at a time.  Anexternal task calls (makes a rendezvous with, in Ada terminology) a TELLER(n)task through its REQUEST entry point, passing one of these transaction names asa parameter, and the TELLER(n) task processes the request.  For example, a codesegment in an external task may look something like Figure CALLTELLER.  Thiscode segment was extracted from the body of the CUSTOMER task definition in thepackage CUSTOMER_WORLD (see later).
  97.     This demonstration program also contains several other tasks, calledcustomer tasks.  These tasks, hidden inside the package CUSTOMER_WORLD, operateindependently and interact with the various teller tasks.  The customer tasksmake requests to the teller tasks, asking to be added as a customer of the bank,making deposits, and making withdrawals.  Using the Ada inter-task rendezvousmechanism to communicate, if the teller task is already servicing anothercustomer task, the customer task "waits in line" (having been placed in theimplicit queue associated with the teller's entry point).  If queued, thecustomer task is serviced after the tasks in the teller's queue in front of himhave been serviced.  Indeed, the model is similar to the real world.
  98.     From the mainline procedure, the user can monitor the bank and itsactivity.  He can obtain a status report from the bank (which includes thebalanace of each customer, the number of transactions requested by eachcustomer, and the number of transactions processed by each teller), cause newcustomer tasks to start up, and shut down the system and exit the program.  Forthose Ada compilers which do not allow tasking to proceed while the current taskis waiting for input (such as many of the Ada compilers available for the IBMPC), a continuous display function is also provided to the user which displaysthe bank's status, delays five seconds (allowing the various tasks to run), anddisplays the bank's status again.  This command option displays ten statusreports before returning the user to his prompt.
  99. RESERVE 4
  100. SECTION Execution of the Program
  101.  
  102.     Figures EX1 to EX7 show various displays during the execution of theprogram.  These displays were generated on a SUN workstation, where the programwas compiled by the Verdix Ada compiler.  These displays may be different fromwhat you see if you compile the program on an IBM PC, particularly in thatVerdix Ada does not suspend all tasks while the current task is waiting forinput from the console.  While we are at the prompt in these figures, all tasksare running in parallel with our input activity.  On some compilers this willnot be the case, and the parallel operation of the tasks will not be seen untilthe user issues the 'c' (continuous bank status) command.
  103.  
  104. BEGIN FIGURE
  105. ======================================================================
  106.  
  107. Enter
  108.   b for bank status
  109.   c for continuous bank status
  110.   s for start next customer
  111.   x for exit: b
  112. The bank doesn't have any customers
  113.  
  114. CAPTION When BD3 starts, there are no customers.
  115. TAG EX1=figure
  116.  
  117. ======================================================================
  118. END FIGURE
  119.  
  120.     Figure EX1 shows the menu that is presented to the user when the programstarts and that no customer tasks are running yet.
  121.  
  122. BEGIN FIGURE
  123. ======================================================================
  124.  
  125. Enter
  126.   b for bank status
  127.   c for continuous bank status
  128.   s for start next customer
  129.